package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.FindConsumerQueryVo;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;

    // 物资去处展示
    @Override
    public PageVO<ConsumerVO> findConsumerList(FindConsumerQueryVo consumerQueryVo) {
        // 当前页和每页记录数
        Integer pageNum = consumerQueryVo.getPageNum();
        Integer pageSize = consumerQueryVo.getPageSize();

        // 查询对象
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerQueryVo,consumer);

        // 去db查询
        List<Consumer> consumerList = consumerMapper.findConsumerList(consumer);

        // 转换对象
        List<ConsumerVO> consumerVOList = consumerList.stream().map(c->{
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(c,consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(consumerVOList);
        PageVO<ConsumerVO> pageVO = new PageVO<>(pageInfo.getTotal(), consumerVOList);

        return pageVO;
    }

    // 添加物资去处
    @Override
    public void addConsumer(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        consumerMapper.insert(consumer);
    }

    // 编辑物资去处-根据id查询
    @Override
    public Consumer editConsumer(Long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        consumer = consumerMapper.selectOne(consumer);
        return consumer;
    }

    // 编辑物资去处
    @Override
    public void updateConsumer(Long id, Consumer consumer) {

        consumer.setId(id);
        consumer.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKey(consumer);
    }

    // 删除物资去处
    @Override
    public void deleteConsumer(long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        consumerMapper.delete(consumer);
    }
}
